using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Input;
using NuiBlue.Bluetooth;

namespace NuiSurfaceExample
{
    /// <summary>
    /// Interaction logic for SurfaceWindow1.xaml
    /// </summary>
    public partial class SurfaceWindow1 : SurfaceWindow
    {
        public delegate void Status(object sender, string status);
        public delegate void downloadedImage(object sender, BluetoothEventArgs e);

        /// <summary>
        /// Default constructor.
        /// </summary>
        public SurfaceWindow1()
        {
            InitializeComponent();
        }

        void bc_dataDownloaded(object sender, BluetoothEventArgs e)
        {
            Dispatcher.BeginInvoke(new downloadedImage(addImage), sender, e);
        }

        void addImage(object sender, BluetoothEventArgs e)
        {
            try
            {
                Image image = new Image();
                image.Source = new BitmapImage(new Uri(e.FilePath));

                MainScatterView.Items.Add(image);
            }
            catch (Exception ex)
            {
                MessageBox.Show("**" + e.FileName + "**" + ex.Message);
            }
        }

        void statusUpdate(object sender, string status)
        {
            txtStatus.Text = status;
        }

        void bc_StatusUpdate(object sender, string status)
        {
            Dispatcher.BeginInvoke(new Status(statusUpdate), sender, status);
        }

        private void SurfaceButton_Click(object sender, RoutedEventArgs e)
        {
            BluetoothController bc = new BluetoothController();
            bc.dataDownloaded += new BluetoothController.DataDownloadedHandler(bc_dataDownloaded);
            bc.StatusUpdate += new BluetoothController.StatusEventHandler(bc_StatusUpdate);
            bc.setAddress("<address>");
            bc.setPin("<pin>");
            bc.setMaxFiles(3);
            bc.downloadData(true, BluetoothController.Type.image, null, null);
        }
    }
}